package com.weishi.project.training.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishi.common.utils.StringUtils;
import com.weishi.project.api.vo.HeartRateCountVo;
import com.weishi.project.training.domain.HeartRateCount;
import com.weishi.project.training.domain.HeartRateRecord;
import com.weishi.project.training.domain.TrainingAchievement;
import com.weishi.project.training.mapper.HeartRateCountMapper;
import com.weishi.project.training.service.IHeartRateCountService;
import com.weishi.project.training.service.IHeartRateRecordService;
import com.weishi.project.training.service.ITrainingAchievementService;
import com.weishi.project.training.vo.HeartRateCountAddVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 心率计次数据Service业务层处理
 */
@Service
public class HeartRateCountServiceImpl extends ServiceImpl<HeartRateCountMapper, HeartRateCount> implements IHeartRateCountService {

    @Autowired
    private HeartRateCountMapper heartRateCountMapper;

    @Autowired
    private IHeartRateRecordService heartRateRecordService;

    @Autowired
    private ITrainingAchievementService trainingAchievementService;


    /**
     * 查询心率计次数据列表
     */
    @Override
    public List<HeartRateCount> selectHeartRateCountList(HeartRateCount heartRateCount) {
        return heartRateCountMapper.selectHeartRateCountList(heartRateCount);
    }

    @Override
    public List<HeartRateCount> qryCountByChannel(Integer channelId) {
        return heartRateCountMapper.selectCountByChannel(channelId);
    }

    @Override
    public void updateByChannelId(HeartRateCount heart) {
        heartRateCountMapper.updateByChannelId(heart);
    }

    @Override
    public List<HeartRateCount> selectHeartRateCountList(Integer groupId, Integer userId) {
        Map<String, Integer> params = new HashMap<>();
        params.put("groupId", groupId);
        params.put("userId", userId);
        return heartRateCountMapper.selectHeartRateCountByMap(params);
    }

    @Override
    public List<HeartRateCountVo> qryCountBySportsman(int channelId, int sportsmanId) {
        Map<String, Object> params = new HashMap<>();
        params.put("channelId", channelId);
        params.put("userId", sportsmanId);
        return heartRateCountMapper.selectCountBySportsman(params);
    }


    @Override
    public void addOrSaveHeartRateCount(List<HeartRateCountAddVo> list) {
        for (HeartRateCountAddVo vo : list) {
            this.saveOrUpdate(vo.getHeartRateCount());
            if (vo.getHeartRateRecords() != null && vo.getHeartRateRecords().size() > 0) {
                for (HeartRateRecord record : vo.getHeartRateRecords()) {
                    record.setHeartRateCountId(vo.getHeartRateCount().getId());
                    heartRateRecordService.saveOrUpdate(record);
                }
            }
        }
    }

    @Override
    public List<HeartRateCountAddVo> qryHeartRateCount(Integer channel) {
        List<HeartRateCountAddVo> voList = new ArrayList<>();
        List<HeartRateCount> list = this.qryCountByChannel(channel);

        Map<String, Object> params = new HashMap<>();
        params.put("channelId", channel);
        List<HeartRateRecord> records = heartRateRecordService.selectByMap(params);

        Map<Integer, List<HeartRateRecord>> temp = new HashMap<>();
        for (HeartRateRecord heartRateRecord : records) {
            List<HeartRateRecord> heartRateRecords = temp.get(heartRateRecord.getHeartRateCountId());
            if (heartRateRecords == null) {
                heartRateRecords = new ArrayList<>();
                temp.put(heartRateRecord.getHeartRateCountId(), heartRateRecords);
            }
            heartRateRecords.add(heartRateRecord);
        }

        for (HeartRateCount heartRateCount : list) {
            HeartRateCountAddVo vo = new HeartRateCountAddVo();
            //vo.setHeartRateCount(heartRateCount);
            vo.setHeartRateRecords(temp.get(heartRateCount.getId()));
            voList.add(vo);
        }
        return voList;
    }



    /**
     * 封装查询水上桨频
     */
    @Override
    public List<HeartRateCount> listZHKID(Integer channelId) {
        return this.list(Wrappers.<HeartRateCount>lambdaQuery()
                .eq(HeartRateCount::getChannelId , channelId));
    }

    @Override
    @Transactional
    public void saveAndAddRecord(HeartRateCount heartRateCount) {
        save(heartRateCount);

        // 查询航道里面所有的人
        List<TrainingAchievement> trainingAchievementList = trainingAchievementService
                .list(Wrappers.<TrainingAchievement>lambdaQuery()
                        .eq(TrainingAchievement::getChannelId, heartRateCount.getChannelId()));

        if (trainingAchievementList.size() > 0) {
            trainingAchievementList.forEach(p -> {
                // 新增运动员心率计次数据
                HeartRateRecord heartRateRecord = new HeartRateRecord();
                heartRateRecord.setHeartRateCountId(heartRateCount.getId());
                heartRateRecord.setSportsmanId(p.getSportsmanId());
                heartRateRecord.setCtime(new Date());
                heartRateRecordService.save(heartRateRecord);
            });
        }
    }
}